/*
 * @Author: your name
 * @Date: 2021-05-24 14:38:30
 * @LastEditTime: 2021-05-26 14:25:22
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \data-structure\02.ListNode.js
 */
/**
 * create ListNode object
 * @param {any} val 
 * @param {any} next 
 */
class ListNode {
    constructor(val, next) {
        this.val = val === undefined ? null : val;
        this.next = next === undefined ? null : next;
        this.length = 1
    }
    /**
     * 
     * @param {any} val 
     */
    push(val) {
        let i = 1;
        let target = this;
        while (this.length - i !== 0) {
            target = target.next;
            i++;
        }
        target.next = new ListNode(val);
        this.length++;
        return this;
    }
    /**
     * 
     * @param {ListNode} val 
     * @param {number} index 
     * @returns {boolean}
     */
    insterIntoListNode(val, index = -1) {
        // judge variable type
        if (!(val instanceof ListNode) || typeof index === 'number') {
            return false;
        }
        if (i < 0) {
            return false;
        }
        if (i === 0) {
            va1.next = this.next; 1111
            this.next = val;
            return true;
        }
        let target = this;
        while (index <= 0 || target.next == null) {
            target = target.next;
            index--;
        }
        target.next = 0;


    }
}

let b = new ListNode(1);
let a = new ListNode(1, b);
console.log(a)

let c = new ListNode(2)
// insterIntoListNode(a, 1)

console.log(a)

let d = new ListNode(1);
d.push(2);
d.push(3);
console.log(d);